home *** CD-ROM | disk | FTP | other *** search
/ PC Format (PL) 2015 August / PC_Format_082015.iso / Pełne wersje / F-Secure FREEDOME VPN 1.0.11 / Freedome.exe / QtGraphicalEffects / private / FastGlow.qml next >
Text File  |  2015-04-13  |  13KB  |  394 lines

  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
  4. ** Contact: http://www.qt-project.org/legal
  5. **
  6. ** This file is part of the Qt Graphical Effects module.
  7. **
  8. ** $QT_BEGIN_LICENSE:BSD$
  9. ** You may use this file under the terms of the BSD license as follows:
  10. **
  11. ** "Redistribution and use in source and binary forms, with or without
  12. ** modification, are permitted provided that the following conditions are
  13. ** met:
  14. **   * Redistributions of source code must retain the above copyright
  15. **     notice, this list of conditions and the following disclaimer.
  16. **   * Redistributions in binary form must reproduce the above copyright
  17. **     notice, this list of conditions and the following disclaimer in
  18. **     the documentation and/or other materials provided with the
  19. **     distribution.
  20. **   * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
  21. **     of its contributors may be used to endorse or promote products derived
  22. **     from this software without specific prior written permission.
  23. **
  24. **
  25. ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  31. ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  33. ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
  36. **
  37. ** $QT_END_LICENSE$
  38. **
  39. ****************************************************************************/
  40.  
  41. import QtQuick 2.0
  42.  
  43. Item {
  44.     id: rootItem
  45.     property variant source
  46.     property real spread: 0.0
  47.     property real blur: 0.0
  48.     property color color: "white"
  49.     property bool transparentBorder: false
  50.     property bool cached: false
  51.  
  52.     SourceProxy {
  53.         id: sourceProxy
  54.         input: rootItem.source
  55.     }
  56.  
  57.     ShaderEffectSource {
  58.         id: cacheItem
  59.         anchors.fill: shaderItem
  60.         visible: rootItem.cached
  61.         smooth: true
  62.         sourceItem: shaderItem
  63.         live: true
  64.         hideSource: visible
  65.     }
  66.  
  67.     property string __internalBlurVertexShader: "
  68.         attribute highp vec4 qt_Vertex;
  69.         attribute highp vec2 qt_MultiTexCoord0;
  70.         uniform highp mat4 qt_Matrix;
  71.         uniform highp float yStep;
  72.         uniform highp float xStep;
  73.         varying highp vec2 qt_TexCoord0;
  74.         varying highp vec2 qt_TexCoord1;
  75.         varying highp vec2 qt_TexCoord2;
  76.         varying highp vec2 qt_TexCoord3;
  77.  
  78.         void main() {
  79.             qt_TexCoord0 = vec2(qt_MultiTexCoord0.x + xStep, qt_MultiTexCoord0.y + yStep * 0.36);
  80.             qt_TexCoord1 = vec2(qt_MultiTexCoord0.x + xStep * 0.36, qt_MultiTexCoord0.y - yStep);
  81.             qt_TexCoord2 = vec2(qt_MultiTexCoord0.x - xStep * 0.36, qt_MultiTexCoord0.y + yStep);
  82.             qt_TexCoord3 = vec2(qt_MultiTexCoord0.x - xStep, qt_MultiTexCoord0.y - yStep * 0.36);
  83.             gl_Position = qt_Matrix * qt_Vertex;
  84.         }
  85.     "
  86.  
  87.     property string __internalBlurFragmentShader: "
  88.         uniform lowp sampler2D source;
  89.         uniform lowp float qt_Opacity;
  90.         varying highp vec2 qt_TexCoord0;
  91.         varying highp vec2 qt_TexCoord1;
  92.         varying highp vec2 qt_TexCoord2;
  93.         varying highp vec2 qt_TexCoord3;
  94.  
  95.         void main() {
  96.             highp vec4 sourceColor = (texture2D(source, qt_TexCoord0) +
  97.             texture2D(source, qt_TexCoord1) +
  98.             texture2D(source, qt_TexCoord2) +
  99.             texture2D(source, qt_TexCoord3)) * 0.25;
  100.             gl_FragColor = sourceColor * qt_Opacity;
  101.         }
  102.    "
  103.  
  104.     ShaderEffect {
  105.         id: level0
  106.         property variant source: sourceProxy.output
  107.         anchors.fill: parent
  108.         visible: false
  109.         smooth: true
  110.     }
  111.  
  112.     ShaderEffectSource {
  113.         id: level1
  114.         width: Math.ceil(shaderItem.width / 32) * 32
  115.         height: Math.ceil(shaderItem.height / 32) * 32
  116.         sourceItem: level0
  117.         hideSource: rootItem.visible
  118.         sourceRect: transparentBorder ? Qt.rect(-64, -64, shaderItem.width, shaderItem.height) : Qt.rect(0,0,0,0)
  119.         smooth: true
  120.         visible: false
  121.     }
  122.  
  123.     ShaderEffect {
  124.         id: effect1
  125.         property variant source: level1
  126.         property real yStep: 1/height
  127.         property real xStep: 1/width
  128.         anchors.fill: level2
  129.         visible: false
  130.         smooth: true
  131.         vertexShader: __internalBlurVertexShader
  132.         fragmentShader: __internalBlurFragmentShader
  133.     }
  134.  
  135.     ShaderEffectSource {
  136.         id: level2
  137.         width: level1.width / 2
  138.         height: level1.height / 2
  139.         sourceItem: effect1
  140.         hideSource: rootItem.visible
  141.         visible: false
  142.         smooth: true
  143.     }
  144.  
  145.     ShaderEffect {
  146.         id: effect2
  147.         property variant source: level2
  148.         property real yStep: 1/height
  149.         property real xStep: 1/width
  150.         anchors.fill: level3
  151.         visible: false
  152.         smooth: true
  153.         vertexShader: __internalBlurVertexShader
  154.         fragmentShader: __internalBlurFragmentShader
  155.     }
  156.  
  157.     ShaderEffectSource {
  158.         id: level3
  159.         width: level2.width / 2
  160.         height: level2.height / 2
  161.         sourceItem: effect2
  162.         hideSource: rootItem.visible
  163.         visible: false
  164.         smooth: true
  165.     }
  166.  
  167.     ShaderEffect {
  168.         id: effect3
  169.         property variant source: level3
  170.         property real yStep: 1/height
  171.         property real xStep: 1/width
  172.         anchors.fill: level4
  173.         visible: false
  174.         smooth: true
  175.         vertexShader: __internalBlurVertexShader
  176.         fragmentShader: __internalBlurFragmentShader
  177.     }
  178.  
  179.     ShaderEffectSource {
  180.         id: level4
  181.         width: level3.width / 2
  182.         height: level3.height / 2
  183.         sourceItem: effect3
  184.         hideSource: rootItem.visible
  185.         visible: false
  186.         smooth: true
  187.     }
  188.  
  189.     ShaderEffect {
  190.         id: effect4
  191.         property variant source: level4
  192.         property real yStep: 1/height
  193.         property real xStep: 1/width
  194.         anchors.fill: level5
  195.         visible: false
  196.         smooth: true
  197.         vertexShader: __internalBlurVertexShader
  198.         fragmentShader: __internalBlurFragmentShader
  199.     }
  200.  
  201.     ShaderEffectSource {
  202.         id: level5
  203.         width: level4.width / 2
  204.         height: level4.height / 2
  205.         sourceItem: effect4
  206.         hideSource: rootItem.visible
  207.         visible: false
  208.         smooth: true
  209.     }
  210.  
  211.     ShaderEffect {
  212.         id: effect5
  213.         property variant source: level5
  214.         property real yStep: 1/height
  215.         property real xStep: 1/width
  216.         anchors.fill: level6
  217.         visible: false
  218.         smooth: true
  219.         vertexShader: __internalBlurVertexShader
  220.         fragmentShader: __internalBlurFragmentShader
  221.     }
  222.  
  223.     ShaderEffectSource {
  224.         id: level6
  225.         width: level5.width / 2
  226.         height: level5.height / 2
  227.         sourceItem: effect5
  228.         hideSource: rootItem.visible
  229.         visible: false
  230.         smooth: true
  231.     }
  232.  
  233.     Item {
  234.         id: dummysource
  235.         width: 1
  236.         height: 1
  237.         visible: false
  238.     }
  239.  
  240.     ShaderEffectSource {
  241.         id: dummy
  242.         width: 1
  243.         height: 1
  244.         sourceItem: dummysource
  245.         visible: false
  246.         smooth: false
  247.         live: false
  248.     }
  249.  
  250.     ShaderEffect {
  251.         id: shaderItem
  252.         x: transparentBorder ? -64 : 0
  253.         y: transparentBorder ? -64 : 0
  254.         width: transparentBorder ? parent.width + 128 : parent.width
  255.         height: transparentBorder ? parent.height + 128 : parent.height
  256.  
  257.         property variant source1: level1
  258.         property variant source2: level2
  259.         property variant source3: level3
  260.         property variant source4: level4
  261.         property variant source5: level5
  262.         property variant source6: level6
  263.         property real lod: rootItem.blur
  264.  
  265.         property real weight1;
  266.         property real weight2;
  267.         property real weight3;
  268.         property real weight4;
  269.         property real weight5;
  270.         property real weight6;
  271.  
  272.         property real spread: 1.0 - (rootItem.spread * 0.98)
  273.         property alias color: rootItem.color
  274.  
  275.         function weight(v) {
  276.             if (v <= 0.0)
  277.                 return 1
  278.             if (v >= 0.5)
  279.                 return 0
  280.  
  281.             return 1.0 - v / 0.5
  282.         }
  283.  
  284.         function calculateWeights() {
  285.  
  286.             var w1 = weight(Math.abs(lod - 0.100))
  287.             var w2 = weight(Math.abs(lod - 0.300))
  288.             var w3 = weight(Math.abs(lod - 0.500))
  289.             var w4 = weight(Math.abs(lod - 0.700))
  290.             var w5 = weight(Math.abs(lod - 0.900))
  291.             var w6 = weight(Math.abs(lod - 1.100))
  292.  
  293.             var sum = w1 + w2 + w3 + w4 + w5 + w6;
  294.             weight1 = w1 / sum;
  295.             weight2 = w2 / sum;
  296.             weight3 = w3 / sum;
  297.             weight4 = w4 / sum;
  298.             weight5 = w5 / sum;
  299.             weight6 = w6 / sum;
  300.  
  301.             upateSources()
  302.         }
  303.  
  304.         function upateSources() {
  305.             var sources = new Array();
  306.             var weights = new Array();
  307.  
  308.             if (weight1 > 0) {
  309.                 sources.push(level1)
  310.                 weights.push(weight1)
  311.             }
  312.  
  313.             if (weight2 > 0) {
  314.                 sources.push(level2)
  315.                 weights.push(weight2)
  316.             }
  317.  
  318.             if (weight3 > 0) {
  319.                 sources.push(level3)
  320.                 weights.push(weight3)
  321.             }
  322.  
  323.             if (weight4 > 0) {
  324.                 sources.push(level4)
  325.                 weights.push(weight4)
  326.             }
  327.  
  328.             if (weight5 > 0) {
  329.                 sources.push(level5)
  330.                 weights.push(weight5)
  331.             }
  332.  
  333.             if (weight6 > 0) {
  334.                 sources.push(level6)
  335.                 weights.push(weight6)
  336.             }
  337.  
  338.             for (var j = sources.length; j < 6; j++) {
  339.                 sources.push(dummy)
  340.                 weights.push(0.0)
  341.             }
  342.  
  343.             source1 = sources[0]
  344.             source2 = sources[1]
  345.             source3 = sources[2]
  346.             source4 = sources[3]
  347.             source5 = sources[4]
  348.             source6 = sources[5]
  349.  
  350.             weight1 = weights[0]
  351.             weight2 = weights[1]
  352.             weight3 = weights[2]
  353.             weight4 = weights[3]
  354.             weight5 = weights[4]
  355.             weight6 = weights[5]
  356.         }
  357.  
  358.         Component.onCompleted: calculateWeights()
  359.  
  360.         onLodChanged: calculateWeights()
  361.  
  362.         fragmentShader: "
  363.             uniform lowp sampler2D source1;
  364.             uniform lowp sampler2D source2;
  365.             uniform lowp sampler2D source3;
  366.             uniform lowp sampler2D source4;
  367.             uniform lowp sampler2D source5;
  368.             uniform mediump float weight1;
  369.             uniform mediump float weight2;
  370.             uniform mediump float weight3;
  371.             uniform mediump float weight4;
  372.             uniform mediump float weight5;
  373.             uniform highp vec4 color;
  374.             uniform highp float spread;
  375.             uniform lowp float qt_Opacity;
  376.             varying mediump vec2 qt_TexCoord0;
  377.  
  378.             highp float linearstep(highp float e0, highp float e1, highp float x) {
  379.                 return clamp((x - e0) / (e1 - e0), 0.0, 1.0);
  380.             }
  381.  
  382.             void main() {
  383.                 lowp vec4 sourceColor = texture2D(source1, qt_TexCoord0) * weight1;
  384.                 sourceColor += texture2D(source2, qt_TexCoord0) * weight2;
  385.                 sourceColor += texture2D(source3, qt_TexCoord0) * weight3;
  386.                 sourceColor += texture2D(source4, qt_TexCoord0) * weight4;
  387.                 sourceColor += texture2D(source5, qt_TexCoord0) * weight5;
  388.                 sourceColor = mix(vec4(0), color, linearstep(0.0, spread, sourceColor.a));
  389.                 gl_FragColor = sourceColor * qt_Opacity;
  390.             }
  391.         "
  392.     }
  393. }
  394.